package com.ciwong.epaper.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ciwong.epaper.bean.City;
import com.ciwong.epaper.util.ESystem;

import java.util.ArrayList;
import java.util.List;

public class Utils {
    /**
     *
     * 查询 省市区 对应 地名
     *
     * @param mContext
     *
     * @param nowAddress
     *            地名代号
     * @param fuhao
     *              fuhao连接符号
     * @return 地址
     */
    public static String getAddress(Context mContext, String nowAddress,
                                    String fuhao) {
        String file = ESystem.getCacheAddressFile(mContext).getAbsolutePath();
        if (nowAddress == null) {
            return "";
        }
        StringBuffer address = new StringBuffer();
        final int provinceLength = 6;
        final int cityLength = 4;
        final int two = 2;
        if (nowAddress.length() == two || nowAddress.length() == cityLength
                || nowAddress.length() == provinceLength) {
            SQLiteDatabase db = SQLiteDatabase.openDatabase(file, null,
                    SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            if (db.isOpen()) {
                if (nowAddress.length() >= two) {
                    Cursor provinCursor = db.rawQuery(
                            "select zonestr from province_city where zonenum=?",
                            new String[]{nowAddress.substring(0, two)});
                    while (provinCursor.moveToNext()) {
                        address = address.append(provinCursor.getString(0));
                    }
                    provinCursor.close();
                }
                if (nowAddress.length() >= cityLength) {
                    Cursor cityCursor = db.rawQuery(
                            "select zonestr from province_city where zonenum=?",
                            new String[]{nowAddress.substring(0, cityLength)});
                    while (cityCursor.moveToNext()) {
                        address = address.append(fuhao + cityCursor.getString(0));
                    }
                    cityCursor.close();
                }
                if (nowAddress.length() >= provinceLength) {
                    Cursor townCursor = db.rawQuery(
                            "select zonestr from province_city where zonenum=?",
                            new String[]{nowAddress});
                    while (townCursor.moveToNext()) {
                        address = address.append(fuhao + townCursor.getString(0));
                    }
                    townCursor.close();
                }
                db.close();
            }
        }
        return address.toString();
    }

    /**
     * 根据类型查询城市
     *
     * @param file 文件
     * @param type (1,省 2,市 3,区)
     * @return 城市
     */
    public static List<City> getCitysByType(String file, int type) {
        List<City> list = new ArrayList<City>();
        SQLiteDatabase db = SQLiteDatabase.openDatabase(file, null,
                SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        if (db.isOpen()) {
            Cursor cursor = db.rawQuery(
                    "select * from province_city where type = ?",
                    new String[]{type + ""});
            while (cursor.moveToNext()) {
                list.add(getCityByCursor(cursor));
            }
            cursor.close();
            db.close();
        }
        return list;
    }

    private static City getCityByCursor(Cursor cur) {
        City city = new City();
        city.setRefrence(cur.getInt(cur.getColumnIndex("refrence")));
        city.setType(cur.getInt(cur.getColumnIndex("type")));
        city.setZoneNum(cur.getInt(cur.getColumnIndex("zonenum")));
        city.setZoneStr(cur.getString(cur.getColumnIndex("zonestr")));
        city.setId(cur.getInt(cur.getColumnIndex("ID")));
        return city;
    }

    /**
     * 获取某一类型城市的名称最大长度
     *
     * @param type 类型
     * @param file 文件地址
     * @return 最大长度
     */
    public static int getMaxLengthOfTyep(int type, String file) {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(file, null,
                SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        if (db.isOpen()) {
            Cursor cursor = db.rawQuery(
                    "select max(length(zonestr)) from province_city where type = ?",
                    new String[]{type + ""});
            while (cursor.moveToNext()) {
                return cursor.getInt(0);
            }
        }
        return 0;
    }

    /**
     * 根据地区码，获取省，市，区索引
     *
     * @param file
     * @param nowAddress
     * @return
     */
    public static List<Integer> getAddressIndex(String file, String nowAddress) {
        if (nowAddress == null) {
            return null;
        }
        List<Integer> indexList = new ArrayList<Integer>();
        int index = 0;
        String refId = 0 + "";
        final int cityLength = 4;
        final int provinceLength = 6;
        final int two = 2;
        if (nowAddress.length() == cityLength) {
            refId = nowAddress.substring(0, two);
            indexList.addAll(getAddressIndex(file, refId));
        } else if (nowAddress.length() == provinceLength) {
            refId = nowAddress.substring(0, cityLength);
            indexList.addAll(getAddressIndex(file, refId));
        }
        SQLiteDatabase db = SQLiteDatabase.openDatabase(file, null,
                SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        if (db.isOpen()) {
            Cursor cursor = db.rawQuery(
                    "select zonenum from province_city where refrence=?",
                    new String[]{refId});
            while (cursor.moveToNext()) {
                if (nowAddress.equals(cursor.getString(0))) {
                    break;
                }
                index++;
            }
            indexList.add(index);
            cursor.close();
            db.close();
        }
        return indexList;
    }

    /**
     * 根据依附关系查询城市
     *
     * @param file     文件
     * @param refrence 依附的城市id
     * @return 城市
     */
    public static List<City> getCitysByRefrence(String file, int refrence) {
        List<City> list = new ArrayList<City>();
        SQLiteDatabase db = SQLiteDatabase.openDatabase(file, null,
                SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        if (db.isOpen()) {
            Cursor cursor = db.rawQuery(
                    "select * from province_city where refrence = ?",
                    new String[]{refrence + ""});
            while (cursor.moveToNext()) {
                list.add(getCityByCursor(cursor));
            }
            cursor.close();
            db.close();
        }
        return list;
    }
}
